import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
  Res,
} from '@nestjs/common';
import { UploadService } from './upload.service';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { join } from 'path';
import { zip } from 'compressing';

@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}

  @Post('album')
  @UseInterceptors(FileInterceptor('file'))
  upload(@UploadedFile() file) {
    console.log(file, 'file');
    return {
      msg: 'hh',
      data: file,
    };
  }

  @Get('export')
  download(@Res() res: Response) {
    const downloadUrl = join(__dirname, '../data/1722587300398.png');
    res.download(downloadUrl);
  }

  @Get('stream')
  async stream(@Res() res: Response) {
    const downloadUrl = join(__dirname, '../data/1722587300398.png');
    const stream = new zip.Stream();
    stream.addEntry(downloadUrl);

    res.setHeader('Content-Type', 'application/octet-stream');

    res.setHeader('Content-Disposition', 'attachment; filename=stream.png');

    stream.pipe(res);
  }
}
